# -*- coding: utf-8 -*-
# @Time    : 2018/2/9 17:15
# @Author  : Soft
# @File    : timeUtils.py
import time
import datetime
from config import Check, WorkUp, Clazz_ST

fmt = '%Y-%m-%d %H:%M'


def get_time(prev=0):
    #_time = datetime.datetime.today() + datetime.timedelta(minutes=prev)
    _time = datetime.datetime.today()
    return _time.strftime(fmt)

def get_year():
    return time.strftime('%Y')


def get_date(prev=0):
    _time = datetime.datetime.today() + datetime.timedelta(days=prev)
    return _time.strftime('%Y-%m-%d')

# 签到时间控制 课程开始1.5小时内都可以签到
def check_limit(k_time):
    start_datetime = datetime.datetime.strptime(k_time, fmt)
    end_datetime = start_datetime + datetime.timedelta(hours=1, minutes=30)
    can_datetime = start_datetime - datetime.timedelta(minutes=30)
    now_datetime = datetime.datetime.now()
    if now_datetime < can_datetime:
        return '签到未开始'
    elif now_datetime > end_datetime:
        return Check.QQ  # 缺勤
    elif now_datetime > start_datetime:
        return Check.CD  # 迟到
    else:
        return Check.CQ  # 正常出勤

# 自习签到控制 在允许时间内均可
def zs_limit(start, end):
    start_datetime = datetime.datetime.strptime(start, fmt)
    end_datetime = datetime.datetime.strptime(end, fmt)
    now_datetime = datetime.datetime.now()
    if start_datetime < now_datetime < end_datetime:
        return True
    return False


# 可以正常提交作业时间控制
def work_limit(k_time, work_endtime):
    start = datetime.datetime.strptime(k_time, fmt)
    limit = datetime.datetime.strptime(work_endtime, fmt)
    now = datetime.datetime.now()
    if now < start:
        return '还没开始提交作业'
    elif now > limit:
        return WorkUp.YQ  # 逾期提交
    return WorkUp.YJ  # 正常提交


# 判断课程状态 课程间隔是结束时间和开始时间差
def get_class_status(k_time, work_end_time):
    now_datetime = datetime.datetime.now()
    end_datetime = datetime.datetime.strptime(work_end_time, fmt)
    start_datetime = datetime.datetime.strptime(k_time, fmt)
    if now_datetime < start_datetime - datetime.timedelta(minutes=40):
        return Clazz_ST.WKS
    elif now_datetime > end_datetime:
        return Clazz_ST.YJS
    else:
        return Clazz_ST.JXZ
